package com.hjc.answer.controller;

import com.hjc.answer.model.UserProj;
import com.hjc.answer.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: HJC
 * Date: 2019/11/14
 * Time: 15:20
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/login")
public class UserCtl {
    @Autowired
    private UserServiceImpl userService;
    @PostMapping("index")
    public String index( HttpServletRequest request , String uid, String password){
        //uid password
        //查询user
        UserProj userProj = userService.user_login(uid,password);
        if(userProj!= null){
            //存入session
            request.getSession().setAttribute("user",userProj);
            //返回视图subjectChoose
            return "redirect:/login/subjectchoose.html";
        }
        HttpSession httpSession = request.getSession(true);
        httpSession.setAttribute("msg","账号或者密码错误，重新输入！");
        //转发到首页
        return "forward:/index.html";
    }

    @RequestMapping("index.html")
    public String indexs(){
        return "../../index";
    }

    @GetMapping("subjectchoose.html")
    public String choose(HttpServletRequest request){
        UserProj user = (UserProj) request.getSession().getAttribute("user");
        if (user == null){
            return "forward:/index.html";
        }
        return "subjectChoose";
    }
}
